package com.rc.fragment;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rc.activity.DashBoardScreen;
import com.rc.activity.ImageViewScreen;
import com.rc.activity.LoadData;
import com.rc.activity.R;
import com.rc.activity.ReceiptBox;
import com.rc.activity.ReceiptBox.OnBackPressedListener;
import com.rc.activity.ReceiptBox.OnOpenSliding;
import com.rc.activity.ReceiptMerchantInfo;
import com.rc.activity.dao.tbl_Count;
import com.rc.activity.dao.tbl_Receipt;
import com.rc.activity.dao.tbl_ReceiptDao;
import com.rc.activity.provider.Contract;
import com.rc.adapter.DialogAdapter;
import com.rc.adapter.ReceiptListAdapter;
import com.rc.db.DatabaseSupport;
import com.rc.object.ReceiptObj;
import com.rc.util.Const;
import com.rc.util.Util;
import com.telpoo.frame.model.BaseModel;
import com.telpoo.frame.ui.BaseFragment;
import com.telpoo.frame.utils.Utils;

@SuppressLint({ "NewApi", "ValidFragment", "SimpleDateFormat" })
public class ReceiptFragment extends BaseFragment implements OnClickListener,
		TextWatcher, OnBackPressedListener, OnOpenSliding {

	private View loadMoreView;

	public TextView txtSelect, txtTitleContent, txtFilterDate, txtSearch;
	public Dialog dialog;

	private View v;
	private RelativeLayout rlOneMonth, rlSelect, rlAllReceipt;
	private TextView txtOneMonth, txtAllReceipt;

	private com.rc.custom.PullToRefreshListView lvReceipt;
	private LinearLayout llSearch, llFuntion, llFuntionSearch, llAction,
			llActionCancel, llAddNew, llSnap, llSearchbtCancel, llDelete;
	private ImageView imgAllReceipt;

	private boolean isCheck = true;
	private ReceiptListAdapter adpReceiptList = null;
	private Cursor cursor;

	private int mCountReceipt = 20;
	private int mReceiptType = 0;
	private int numberFilter = 0;
	private long start = 0;
	private long end = 0;
	private boolean myReceiptReceiverIsRegistered = false;

	private ReceiptBox receiptBox;
	private ArrayList<ReceiptObj> arrObjReceipt;
	private ReceiptUpdateBroadcast receiptBroadcast;

	int currentFirstVisibleItem, currentVisibleItemCount, currentScrollState;

	public ReceiptFragment() {
		super();
	}

	public ReceiptFragment(int ReceiptType, int mCountReceipt, int numberFilter) {
		super();
		this.mCountReceipt = mCountReceipt;
		this.numberFilter = numberFilter;
		this.mReceiptType = ReceiptType;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		// Create View, setTittle, setBackground for View
		v = (ViewGroup) inflater.inflate(R.layout.receiptlist, null);
		((ReceiptBox) getActivity())
				.setOnBackPressedListener(new ReceiptFragment(mReceiptType,
						mCountReceipt, numberFilter));
		((ReceiptBox) getActivity()).setOnOpenSliding(new ReceiptFragment(
				mReceiptType, mCountReceipt, numberFilter));

		((ReceiptBox) getActivity()).setLogo(R.drawable.ic_top_receiptbox);
		((ReceiptBox) getActivity()).setBackground(
				R.color.background_title_receipt, getResources());
		// Register

		lvReceipt = (com.rc.custom.PullToRefreshListView) v
				.findViewById(R.id.PullToRefresh);
		txtOneMonth = (TextView) v.findViewById(R.id.txtOneMonth);
		rlOneMonth = (RelativeLayout) v.findViewById(R.id.rlOneMonth);
		rlSelect = (RelativeLayout) v.findViewById(R.id.rlSelect);
		llSearch = (LinearLayout) v.findViewById(R.id.llSearch);
		llFuntion = (LinearLayout) v.findViewById(R.id.llFuntion);
		llFuntionSearch = (LinearLayout) v.findViewById(R.id.llFuntionSearch);
		llSearchbtCancel = (LinearLayout) v.findViewById(R.id.llSearchbtCancel);
		txtSearch = (TextView) v.findViewById(R.id.txtSearch);
		llAction = (LinearLayout) v.findViewById(R.id.llAction);
		llActionCancel = (LinearLayout) v.findViewById(R.id.llActionCancel);
		llAddNew = (LinearLayout) v.findViewById(R.id.llAddNew);
		txtSelect = (TextView) v.findViewById(R.id.txtSelect);
		imgAllReceipt = (ImageView) v.findViewById(R.id.imgAllReceipt);
		llSnap = (LinearLayout) v.findViewById(R.id.llSnap);
		txtAllReceipt = (TextView) v.findViewById(R.id.txtAllReceipt);
		rlAllReceipt = (RelativeLayout) v.findViewById(R.id.rlAllReceipt);
		llDelete = (LinearLayout) v.findViewById(R.id.llDelete);
		txtTitleContent = (TextView) v.findViewById(R.id.txtTitleContent);
		txtFilterDate = (TextView) v.findViewById(R.id.txtFilterDate);

		// Funtion
		rlOneMonth.setOnClickListener(this);
		rlSelect.setOnClickListener(this);
		rlAllReceipt.setOnClickListener(this);
		llFuntionSearch.setOnClickListener(this);
		llSearchbtCancel.setOnClickListener(this);
		txtSearch.addTextChangedListener(this);
		llDelete.setOnClickListener(this);
		llAction.setOnClickListener(this);
		llActionCancel.setOnClickListener(this);
		llAddNew.setOnClickListener(this);
		llSnap.setOnClickListener(this);

		receiptBroadcast = new ReceiptUpdateBroadcast();
		loadMoreView = ((LayoutInflater) getActivity().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.main_loadmore, null, false);
		lvReceipt.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				currentScrollState = scrollState;
				isScrollCompleted();
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				currentFirstVisibleItem = firstVisibleItem;
				currentVisibleItemCount = visibleItemCount;
			}
		});
		return v;
	}

	private void isScrollCompleted() {
		if (currentVisibleItemCount > 0) {

		}
	}

	@Override
	public void onResume() {
		super.onResume();

		if (!myReceiptReceiverIsRegistered) {
			getActivity().registerReceiver(receiptBroadcast,
					new IntentFilter("update.receipt"));
			myReceiptReceiverIsRegistered = true;
		}

		String filterDate = "";
		switch (numberFilter) {
		case 0:
			filterDate = getString(R.string.all_date);
			break;
		case 7:
			filterDate = getString(R.string.one_week);
			break;
		case 14:
			filterDate = getString(R.string.two_weeks);
			break;
		case 30:
			filterDate = getString(R.string.one_month);
			break;
		case 90:
			filterDate = getString(R.string.three_months);
			break;

		default:
			break;
		}
		txtOneMonth.setText(filterDate);

		addDateFilter();
		receiptBox = (ReceiptBox) getActivity();
		receiptBox.setTitle(getString(R.string.receipt_box));
		receiptBox.setBackground(R.color.background_title_receipt,
				getResources());
		// get Receipt on SQLite
		arrObjReceipt = new ArrayList<ReceiptObj>();

		// get by sort
		// arrObjReceipt = DatabaseSupport.getReceiptSQLite(mCountReceipt,
		// mReceiptType, start, end, getActivity());
		arrObjReceipt = DatabaseSupport.getReceiptSQLite(mReceiptType, start,
				end, getActivity());
		int count = 0;
		count = lvReceipt.getFooterViewsCount();
		if (arrObjReceipt.size() > 0 && count == 0) {
			// Init LoadMore
			// if (Integer.valueOf(mCountReceipt) == arrObjReceipt.size()) {

			lvReceipt.addFooterView(loadMoreView);
			loadMoreView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					LoadData.callIntemt(getActivity(), 3);
				}
			});

		}

		// Adapter ListView
		adpReceiptList = new ReceiptListAdapter(getActivity(), arrObjReceipt,
				mReceiptType, mCountReceipt, numberFilter);
		lvReceipt.setAdapter(adpReceiptList);

		// setText filter receipt
		txtAllReceipt.setText(Util.getReceiptTypeByInt(getActivity(),
				mReceiptType));
		txtTitleContent.setText(Util.getReceiptTypeByInt(getActivity(),
				mReceiptType));
		// }
		lvReceipt.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CheckBox cbMulti = (CheckBox) lvReceipt.getChildAt(position).findViewById(R.id.cbMulti);
				showToast(cbMulti.getTag()+"");
			}
		});
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// Search ListView
		if (txtSearch.getText().toString().length() > 0) {
			ReceiptFragment.this.adpReceiptList.notiB(
					searchListView(arrObjReceipt, txtSearch.getText()
							.toString()), mReceiptType, mCountReceipt,
					numberFilter);
		} else {
			resetDataListView(getActivity());
		}

	}

	@Override
	public void onPause() {
		super.onPause();
		if (myReceiptReceiverIsRegistered) {
			getActivity().unregisterReceiver(receiptBroadcast);
			myReceiptReceiverIsRegistered = false;
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		String text_filter = "";
		switch (v.getId()) {
		case R.id.rlOneMonth:
			createDialogFilterTime();
			break;
		case R.id.rlSelect:
			visiableSelect(!adpReceiptList.isShowCheck);
			break;
		case R.id.llFuntionSearch:
			llSearch.setVisibility(View.VISIBLE);
			llFuntion.setVisibility(View.GONE);
			break;
		case R.id.llSearchbtCancel:
			displayFuntionBarBottom();
			resetDataListView(getActivity());
			txtSearch.setText("");
			txtSelect.setText(getString(R.string.select));
			llSearch.setVisibility(View.GONE);
			llFuntion.setVisibility(View.VISIBLE);
			Utils.hideKeyboard(getActivity(), (EditText) txtSearch);
			break;
		case R.id.llActionCancel:
			displayFuntionBarBottom();
			break;
		case R.id.llAddNew:
			ReceiptMerchantInfo.callIntentNew(getActivity(), true);
			break;
		case R.id.llSnap:
			createDialogChoosePhoto();
			break;
		case R.id.rlAllReceipt:
			createDialogFilterReceipt();
			break;
		case R.id.llDelete:
			if (getCheckBoxList()) {
				createDialogMultiDeleteArchie(true);
			} else {
				showToast(getString(R.string.please_select_one_receipt));
			}

			break;
		}
		if (!"".equals(text_filter)) {
			txtOneMonth.setText(text_filter);
			;
		}
	}

	private boolean getCheckBoxList() {
		for (ReceiptObj objReceipt : arrObjReceipt) {
			if (objReceipt.isCheckbox() == true) {
				return true;
			}
		}
		return false;
	}

	/**
	 * MultiDelete receipt in SQLite
	 */
	private int multiUpdate(ArrayList<ContentValues> arrContentValues) {
		int row = 0;

		for (ContentValues values : arrContentValues) {
			row = row
					+ getActivity()
							.getContentResolver()
							.update(Contract.CONTENT_URI_RECEIPT,
									values,
									tbl_ReceiptDao.Properties.ReceiptID.columnName
											+ " = "
											+ values.getAsLong(tbl_ReceiptDao.Properties.ReceiptID.columnName),
									null);
		}
		return row;

	}

	/**
	 * Display Button bar at bottom
	 */
	private void displayFuntionBarBottom() {
		txtSelect.setText(getString(R.string.select));
		imgAllReceipt.setImageResource(R.drawable.ic_selectitem);
		llAction.setVisibility(View.GONE);
		llFuntion.setVisibility(View.VISIBLE);
		adpReceiptList.isShowCheck = false;
		adpReceiptList.notifyDataSetChanged();
		llSearch.setVisibility(View.GONE);
		toggleCheck(adpReceiptList.isShowCheck);
	}

	/**
	 * Search trong listview
	 * 
	 */

	public ArrayList<ReceiptObj> searchListView(
			ArrayList<ReceiptObj> arrReceiptBox, String contentSearch) {
		if (arrReceiptBox == null) {
			return null;
		}
		ArrayList<ReceiptObj> _arrReceiptList = new ArrayList<ReceiptObj>();

		for (int i = 0; i < arrReceiptBox.size(); i++) {
			if (arrReceiptBox.get(i).getMerchantName() != null) {
				if ((String.valueOf(arrReceiptBox.get(i).getTotal()))
						.toUpperCase().contains(contentSearch.toUpperCase())
						|| arrReceiptBox.get(i).getReceiptType().toUpperCase()
								.contains(contentSearch.toUpperCase())
						|| arrReceiptBox.get(i).getMerchantName().toUpperCase()
								.contains(contentSearch.toUpperCase())) {
					_arrReceiptList.add(arrReceiptBox.get(i));
				}
			}

		}
		return _arrReceiptList;
	}

	/**
	 * ẩn hiện multiselect
	 */
	private void visiableSelect(boolean mIsSelect) {
		if (mIsSelect) {
			llAction.setVisibility(View.VISIBLE);
			llFuntion.setVisibility(View.GONE);
			adpReceiptList.isShowCheck = true;
			isCheck = false;
			imgAllReceipt.setImageResource(R.drawable.ic_selectitem);
			txtSelect.setText(getString(R.string.select_all));
			adpReceiptList.notifyDataSetChanged();

		} else {
			setImgSelect(isCheck);

		}

	}

	/**
	 * disalbe và enable CheckBox
	 */
	public void toggleCheck(Boolean val) {
		for (int i = 0; i < lvReceipt.getChildCount(); i++) {
			CheckBox cbMulti = null;
			cbMulti = (CheckBox) lvReceipt.getChildAt(i).findViewById(
					R.id.cbMulti);
			if (cbMulti != null)
				cbMulti.setChecked(val);
		}
	}

	/**
	 * setImage cho ImgAllReceipt và setText cho txtSelect
	 */
	private void setImgSelect(boolean isSelect) {
		toggleCheck(!isSelect);
		if (isSelect) {
			imgAllReceipt.setImageResource(R.drawable.ic_selectitem);
			txtSelect.setText(getString(R.string.select_all));
			isCheck = false;
			for (ReceiptObj receipt : arrObjReceipt) {
				receipt.setCheckbox(false);
			}
		} else {
			imgAllReceipt.setImageResource(R.drawable.ic_selectall);
			txtSelect.setText(getString(R.string.unselect_all));
			isCheck = true;
			for (ReceiptObj receipt : arrObjReceipt) {
				receipt.setCheckbox(true);
			}
		}

	}

	protected Uri imageUri;

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == Const.CAMERA_REQUEST
				&& resultCode == Activity.RESULT_OK) {

			ImageViewScreen
					.callIntentBitMap(getActivity(), imageUri.toString());
		} else if (requestCode == Const.GALLERY_REQUEST
				&& resultCode == Activity.RESULT_OK) {
			try {
				// We need to recyle unused bitmaps
				Log.e("adad", data.getData().toString());
				ImageViewScreen.callIntentBitMap(getActivity(), data.getData()
						.toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void doBack(Activity at) {
		DashBoardScreen.callIntent(at);
	}

	/**
	 * Filter receipt by time
	 */
	private void createDialogFilterTime() {
		dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_receipt_choosetime);
		final ListView lvDialog = (ListView) dialog.findViewById(R.id.lvDialog);
		ArrayList<tbl_Count> arrCount = new ArrayList<tbl_Count>();
		String[] lTime = getResources().getStringArray(R.array.filter_time);
		for (String item : lTime) {
			tbl_Count objCount = new tbl_Count();
			objCount.setName(item);
			objCount.setTotal("");
			arrCount.add(objCount);
		}
		DialogAdapter adpDialog = new DialogAdapter(getActivity(), arrCount,
				txtOneMonth.getText().toString());
		lvDialog.setAdapter(adpDialog);
		lvDialog.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				txtOneMonth
						.setText(((TextView) lvDialog.getChildAt(position)
								.findViewById(R.id.txtTextDialog)).getText()
								.toString());
				int number = 0;
				switch (position) {
				case 0:
					number = 7;
					txtFilterDate.setText(getString(R.string.one_week));
					break;
				case 1:
					number = 14;
					txtFilterDate.setText(getString(R.string.two_weeks));
					break;
				case 2:
					number = 30;
					txtFilterDate.setText(getString(R.string.one_month));
					break;
				case 3:
					number = 90;
					txtFilterDate.setText(getString(R.string.three_months));
					break;
				case 4:
					number = 0;
					txtFilterDate.setText(getString(R.string.all_date));
					break;
				default:
					break;
				}
				numberFilter = number;
				addDateFilter();
				resetDataListView(getActivity());
			}
		});
		((TextView) dialog.findViewById(R.id.txtTitle))
				.setText(getString(R.string.filter_receipts_by_time));
		dialog.show();
	}

	private void resetDataListView(Context ct) {
		arrObjReceipt = new ArrayList<ReceiptObj>();

		// arrObjReceipt = DatabaseSupport.getReceiptSQLite(mCountReceipt,
		// mReceiptType, start, end, getActivity());

		arrObjReceipt = DatabaseSupport.getReceiptSQLite(mReceiptType, start,
				end, ct);
		if (arrObjReceipt == null) {
			return;
		}
		if (adpReceiptList != null) {
			adpReceiptList.notiB(arrObjReceipt, mReceiptType, mCountReceipt,
					numberFilter);
			// if (Integer.valueOf(mCountReceipt) > arrObjReceipt.size()) {
			// lvReceipt.removeFooterView(loadMoreView);
			// }
		}

	}

	private void addDateFilter() {
		if (numberFilter == 0) {
			start = 0;
			end = 0;
			return;
		}
		arrObjReceipt = new ArrayList<ReceiptObj>();
		Timestamp timestamp = new Timestamp(Calendar.getInstance().getTime()
				.getTime());
		end = timestamp.getTime();
		start = Util.removeDateTime(numberFilter);
		// SimpleDateFormat formatdate = new
		// SimpleDateFormat("yyyy-MM-dd-HH-mm");
		// String datenow = "";
		// datenow = formatdate.format(cal.getTime());
		// end = Util.parseDateForReceipt(datenow);
		// String datefrom = Util.removeDateTime(numberFilter,
		// "yyyy-MM-dd-HH-mm");
		// start = Util.parseDateForReceipt(datefrom);
	}

	/**
	 * Lọc Receipt theo kiểu receipt.
	 */

	private void createDialogFilterReceipt() {
		dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_receipt_choosetime);
		final ListView lvDialog = (ListView) dialog.findViewById(R.id.lvDialog);
		DialogAdapter adpDialog = new DialogAdapter(getActivity(),
				DatabaseSupport.getArrReceiptTypes(getResources(),
						getActivity()), txtAllReceipt.getText().toString());
		lvDialog.setAdapter(adpDialog);
		lvDialog.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dialog.dismiss();
				String receiptType = ((TextView) lvDialog.getChildAt(position)
						.findViewById(R.id.txtTextDialog)).getText().toString();
				txtAllReceipt.setText(receiptType);
				txtTitleContent.setText(receiptType);
				mReceiptType = Util.getReceiptTypeByText(getActivity(),
						receiptType);
				resetDataListView(getActivity());
			}
		});
		dialog.show();
	}

	/**
	 * dialog gồm 2 lựa chọn. user có thể chọn tới phần chụp ảnh hoặc user có
	 * thể chọn tới sdcard
	 */

	private void createDialogChoosePhoto() {
		dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_choose_photo);
		LinearLayout llViewAnblum, llTakePicture;
		llViewAnblum = (LinearLayout) dialog.findViewById(R.id.llViewAnblum);
		llTakePicture = (LinearLayout) dialog.findViewById(R.id.llTakePicture);
		llViewAnblum.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(
						Intent.createChooser(intent, "Select Picture"),
						Const.GALLERY_REQUEST);
			}
		});
		llTakePicture.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent cameraIntent = new Intent(
						android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				imageUri = Uri.fromFile(new File(Environment
						.getExternalStorageDirectory(), "receipt"
						+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
				cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
						imageUri);
				startActivityForResult(cameraIntent, Const.CAMERA_REQUEST);
			}
		});
		dialog.show();

	}

	/**
	 * dialog gồm 2 lựa chọn. user có thể chọn tới phần chụp ảnh hoặc user có
	 * thể chọn tới sdcard
	 */

	private void createDialogMultiDeleteArchie(final boolean isDelete) {
		dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_message);

		LinearLayout llOk, llCancel;
		((TextView) dialog.findViewById(R.id.txtTitle))
				.setText(getString(R.string.delete_receipt));
		String mContent = getString(R.string.are_you_sure_you_want_to_delete_receipts);
		((TextView) dialog.findViewById(R.id.txtContent)).setText(mContent);

		llOk = (LinearLayout) dialog.findViewById(R.id.llOk);
		llCancel = (LinearLayout) dialog.findViewById(R.id.llCancel);
		llOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ArrayList<ContentValues> arrContentValues = new ArrayList<ContentValues>();

				for (int i = 0; i < arrObjReceipt.size(); i++) {
					if (arrObjReceipt.get(i).isCheckbox() == true) {
						cursor = getActivity().getContentResolver().query(
								Contract.CONTENT_URI_RECEIPT,
								null,
								tbl_ReceiptDao.Properties.ReceiptID.columnName
										+ " = '"
										+ arrObjReceipt.get(i).getReceiptID()
										+ "'", null, null);
						getActivity().getContentResolver().delete(
								Contract.CONTENT_URI_RECEIPT,
								tbl_ReceiptDao.Properties.ReceiptID.columnName
										+ " = '"
										+ arrObjReceipt.get(i).getReceiptID()
										+ "'", null);
						if (cursor != null) {
							ArrayList<tbl_Receipt> arrReceipt = new ArrayList<tbl_Receipt>();
							arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
							for (tbl_Receipt receipt : arrReceipt) {
								if (isDelete) {
									if (DatabaseSupport.ruleRecipt(
											receipt.getReceiptID(),
											getActivity())) {
										receipt.setIsDelete("1");
										arrContentValues.add(receipt
												.getContentValues());
									} else {
										showToast("" + receipt.getReceiptID());
									}

								} else {
									receipt.setIsUpdate("1");
									receipt.setIsArchived(1);
									arrContentValues.add(receipt
											.getContentValues());
								}

							}
						}
					}
				}
				multiUpdate(arrContentValues);
				resetDataListView(getActivity());
				dialog.dismiss();
			}
		});
		llCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();

	}

	/**
	 * Get BaseModel
	 */

	protected BaseModel getModel() {
		return receiptBox.model;
	}

	@Override
	public void doOpen(Activity at) {
		Util.HidenKeyBoard(at);
	}

	private class ReceiptUpdateBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.e("push-update-receipt", "1");
			resetDataListView(context);
		}

	}

}
